<?php

   class CommonAction extends Action{
   
      public function _initialize(){
	  
	    if(session('did')==""){
		
		  $this->redirect('Login/login');
		
		}
	  
	    $this->yongjin=M('yongjin')->where('id=1')->find();
	
	  }
	  
	   public function duanlianjie($langurl=""){
	  
	   /* $url="http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=".$langurl;
	
	    $result=file_get_contents($url);
	 
	    $result=json_decode($result,true);
	 
	    return $result[0]['url_short'];*/
		
		
		//dump($langurl);die;
		
		$token=$this->token();
	    
		$data['action']="long2short";
		$data['long_url']=$langurl;
		
		$data=json_encode($data);
		
		$url="https://api.weixin.qq.com/cgi-bin/shorturl?access_token=".$token;
		
		$re=$this->https_request($url,$data);
		
		$re=json_decode($re);

	    return $re->short_url;
		
	  
	  }
	  
	  
	  
	    public function token(){
	  
	    $mytoken=M('token')->where('id=1')->find();
		
		if($mytoken['time']<time() or $mytoken['token']==""){
          		//$sys=M('system')->where('id=1')->find();
				$wx=M('wx')->where('id="%s"',1)->find();
		
		        //$wx=M('wx')->where('id=6')->find();
		
		        $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$wx['appid']."&secret=".$wx['appsecret'];
				
				$json=file_get_contents($TOKEN_URL);
				
				
				$result=json_decode($json);
				
				
				$ACC_TOKEN=$result->access_token;
			
				$st['token']=$ACC_TOKEN;
				$st['time']=time()+3600;
				M('token')->where('id=1')->save($st);
		
		
		        $token=$ACC_TOKEN;
		
		}else{
		
		
		  $token=$mytoken['token'];
		
		}
		
		
	     return $token;
	  
	  }
	  
	  
	    public function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
   
	  
	  
	  
	  
	  //分页类  table 表名，num 每页显示条数
	  public function listpage($table,$num,$order='',$where=''){
	    
		   $pagemodel=M($table);
		   import('ORG.Util.Page');
		   $count=$pagemodel->where($where)->count();
		   $page=new page($count,$num);
		   $limit=$page->firstRow.','.$page->listRows;
		   $re=$pagemodel->order($order)->limit($limit)->where($where)->select();
		   $this->re=$re;
		   $this->page=$page->show();
		 
	  }
	  
	  
	   public function page($table,$num,$order='',$where=''){
	    
		   $pagemodel=M($table);
		   import('ORG.Util.Page');
		   $count=$pagemodel->where($where)->count();
		   $page=new page($count,$num);
		   $limit=$page->firstRow.','.$page->listRows;
		   $re=$pagemodel->order($order)->limit($limit)->where($where)->select();
		   //$this->re=$re;
		   $this->page=$page->show();
		   return $re;
		 
	  }
	  //图片上传类
	  
	  public function upload($path){
	      
	        import('ORG.Net.UploadFile');
		    $config['savePath']       = "$path";
			$config['thumb']          = true;
		   
			$upload = new UploadFile($config);
            if($upload->upload()){
		    $info=$upload->getUploadFileInfo();
		    }else{
			$upload->getErrorMsg();
			}
			
			return $info;
	    
	  }
	  
	  
	  public function verify(){
			import('ORG.Util.Image');
			ob_clean();
			Image::buildImageVerify();
		}

	  
	  
	  //找出所有栏目 id 顶级栏目ID
	  
	  
	  
	    public function sel($cata,$mark='--',$pid=0,$level=0){
	  
	     $arr=array();
		 
		 foreach($cata as $v ){
		   
		    if($v['cid']==$pid){
			    $v['level']=$level+1;
			   $v['mark']=str_repeat($mark,$level);
			   $arr[]=$v;
			   $arr=array_merge($arr,$this->sel($cata,$mark,$v['id'],$level+1));
			}
		  }
		 return $arr;
	  }
	  
	  
	    public function sonid($cata,$pid){
  
      $arr=array();
	 // dump($cata);die;
	  foreach($cata as $v){
	    
	     if($v['topid']==$pid){
		   $arr[]=$v['id'];
		  
		   $arr=array_merge($arr,$this->sonid($cata,$v['id']));
		   
		 }
		 //dump($v);
	     
	  }
	 // dump($arr);
    return $arr;
  
  }
   
	   
   
   }